//index.js
//获取应用实例
const app = getApp()
const request = require("../../utils/request.js")
const util = require("../../utils/util.js")
Page({
  data: {
    address_list: [
    ],
    hasAddr: false
  },

  onLoad: function () {

  },
  onUnLoad: function () {
    //TODO 写入地址列表，把设置成默认地址的放入第一个
  },
  onReady: function () {
    // //删除地址后重新请求并刷新地址栏
    // var that = this;
    // var url = request.URL + "/user/address";
    // that.requestAddressList(url, that);
  },
  onShow: function () {
    //请求获取 地址列表
    var that = this;
    var url = request.URL + "/user/address";
    request.request({
      url: url,
      data: {
        userId: 1
      },
      success: function (res) {
        var address_list = res.data;
        console.log('address_list条目数：' + address_list.length);
        if (address_list.length > 0) {
          that.setData({
            hasAddr: true,
            address_list: address_list
          })
        } else {
          that.setData({
            hasAddr: false,
            address_list: []
          })
        }
      },
      fail: function () {
        request.showFailInfo()
      }
    })
  },
  //添加收货地址
  addAddress: function (e) {
    var that = this;
    wx.showActionSheet({
      itemList: ["获取微信地址", "添加收货地址"],
      success: function (res) {
        var idx = res.tapIndex;
        if (idx == 0) {
          if (wx.chooseAddress) {
            wx.chooseAddress({
              success: function (res) {
                var province = res.provinceName;
                var city = res.cityName;
                var district = res.countyName;
                var country = "中国";
                var buyerName = util.trimStartAndEnd(res.userName);
                var buyerPhoneNum = util.trimStartAndEnd(res.telNumber);
                var detail = util.trimStartAndEnd(res.detailInfo);
                var postCode = util.trimStartAndEnd(res.postalCode);
                var url = request.URL + "/user/address/add";
                request.request({
                  url: url,
                  data: {
                    userId: 1,
                    country: country,
                    province: province,
                    city: city,
                    district: district,
                    detail: detail,
                    buyerName: buyerName,
                    buyerPhoneNum: buyerPhoneNum,
                    postcode: postCode,
                  },
                  success: function (res) {
                    that.onShow();
                    console.log(res);
                  },
                  fail: function () {
                    request.showFailInfo()
                  }
                })
              },
              fail: function (err) {
                console.log(JSON.stringify(err))
              }
            })
          } else {
            console.log('当前微信版本不支持chooseAddress');
          }
        } else {
          wx.navigateTo({
            url: '/pages/address_list/add_address/add_address',
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  //编辑地址
  editAddress: function (e) {
    var currentInfo = e.currentTarget.dataset.info;
    console.log(currentInfo)
    var countryName = currentInfo.country;
    var provinceName = currentInfo.province;
    var cityName = currentInfo.city;
    var countyName = currentInfo.district;
    var detailInfo = currentInfo.detail;
    var telNumber = currentInfo.buyerPhoneNum;
    var userName = currentInfo.buyerName;
    var addressId = currentInfo.addressId;
    var postCode = currentInfo.postcode;
    wx.navigateTo({
      url: '/pages/address_list/edit_address/edit_address' + '?countryName=' + countryName + '&provinceName=' + provinceName + '&cityName=' + cityName + '&countyName=' + countyName + '&detailInfo=' + detailInfo + '&postCode=' + postCode + '&telNumber=' + telNumber + '&userName=' + userName + '&addressId=' + addressId
    })
  },
  //删除地址
  deleteAddress: function (e) {
    var that = this;
    wx.showModal({
      title: '确定删除？',
      content: '地址被删除后，如果还需要用到，需要重新添加',
      confirmColor: '#133985',
      cancelColor: 'red',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var url = request.URL + "/user/address/delete";
          var addressId = e.currentTarget.dataset.id;
          request.request({
            url: url,
            data: {
              addressId: addressId
            },
            success: function (res) {
              console.log('address_list条目数：' + that.data.address_list.length);
              that.onShow();
            },
            fail: function () {
              request.showFailInfo()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})

